package com.example.mypotiota_si.Adapter;


import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mypotiota_si.Datas.Bmob_shopgouwuche;
import com.example.mypotiota_si.Datas.Shop_dates_io;
import com.example.mypotiota_si.R;
import com.example.mypotiota_si.Tool.Tools;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class Dp_shop_dates extends RecyclerView.Adapter<Dp_shop_dates.ViewHolder> {

    private static final String TAG = "Dp_shop_dates";
    ArrayList<Shop_dates_io> shop_dates_ios;
    Context context;
    SQLiteDatabase sqLiteDatabase;
    Cursor cursor;
    private ContentValues contentValues;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private int s_int;
    private String id_shop_ID;

    public Dp_shop_dates(SQLiteDatabase sqLiteDatabase, Cursor cursor, ArrayList<Shop_dates_io> shop_dates_ios, Context context) {
        this.shop_dates_ios = shop_dates_ios;
        this.context = context;
        this.sqLiteDatabase = sqLiteDatabase;
        this.cursor = cursor;
    }

    @NonNull
    @Override
    public Dp_shop_dates.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.shop_dates_adapter_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Dp_shop_dates.ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        sharedPreferences = context.getSharedPreferences("sqli",Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        Glide.with(context).load(shop_dates_ios.get(position).getImager_address()).placeholder(R.mipmap.emei_looper2).into(holder.shop_dates_image);
        holder.shop_dates_name.setText(shop_dates_ios.get(position).getName());
        holder.shop_dates_pic.setText(shop_dates_ios.get(position).getPic());
        holder.shop_number.setText(shop_dates_ios.get(position).getAdd_shop()+"");
        String dwa=shop_dates_ios.get(position).getBeizhu();
        //如果备注为null 就不显示
        if(dwa.equals("null")){
            holder.shop_dates_beizhu.setVisibility(View.GONE);
        }else{
            holder.shop_dates_beizhu.setText("备注:"+dwa);
        }

        if(shop_dates_ios.get(position).getAdd_shop() != 0){
            holder.shop_jian.setVisibility(View.VISIBLE);
        }else{
            holder.shop_jian.setVisibility(View.GONE);
        }
        holder.shop_dates_zan.setText("赞:"+shop_dates_ios.get(position).getZan());
        holder.shop_dates_yueshou.setText("月售:"+shop_dates_ios.get(position).getYueshou());
        //加加
        holder.shop_dates_adds_gwc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pluss(v);
            }

            //加加
            private void pluss(View view) {
                Tools.zoomView(view);
                s_int = shop_dates_ios.get(position).getAdd_shop();
                shop_dates_ios.get(position).setAdd_shop(++s_int);
                holder.shop_number.setText(shop_dates_ios.get(position).getAdd_shop()+"");
                //吧商品信息写入云端数据库
                Bmob_shopgouwuche bmob_shopgouwuche =new Bmob_shopgouwuche();
                bmob_shopgouwuche.setShop_imager_address(shop_dates_ios.get(position).getImager_address());
                bmob_shopgouwuche.setShop_sqlitedt_pic(shop_dates_ios.get(position).getPic());
                bmob_shopgouwuche.setShop_sqlitename(shop_dates_ios.get(position).getName());
                bmob_shopgouwuche.setShop_sqliteyueshou(shop_dates_ios.get(position).getYueshou());
                bmob_shopgouwuche.setShop_sqlitenumber(shop_dates_ios.get(position).getAdd_shop()+"");

                BmobQuery<Bmob_shopgouwuche> shopgouwucheBmobQuery=new BmobQuery<>();
                shopgouwucheBmobQuery.findObjects(new FindListener<Bmob_shopgouwuche>() {
                    @Override
                    public void done(List<Bmob_shopgouwuche> list, BmobException e) {
                        if(e==null){
                            for (int i = 0; i < list.size(); i++) {
                                //获取名称
                                String name=list.get(i).getShop_sqlitename();
                                //判断是否有一样的名字
                                if(shop_dates_ios.get(position).getName().equals(name)){
                                    //获取云端的一个id
                                    id_shop_ID = list.get(i).getObjectId();
                                    //更新数据
                                    bmob_shopgouwuche.update(id_shop_ID,new UpdateListener() {
                                        @Override
                                        public void done(BmobException e) {
                                            if(e==null){
                                            }else{
                                                s_int=0;
                                                Toast.makeText(context, "更新失败", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }else if(i==list.size()-1){
                                    //如果循环一圈后没有找到相对应的名称的商品就添加这个商品
                                    bmob_shopgouwuche.save(new SaveListener<String>() {
                                        @Override
                                        public void done(String s, BmobException e) {
                                            if(e==null){
                                                Toast.makeText(context, "添加进购车啦"+s, Toast.LENGTH_SHORT).show();
                                            }else{
                                                s_int=0;
                                                Toast.makeText(context, "购物车异常", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }
                            }
                            if(list.size()==0){
                                //如果查询没有数据时就添加进去
                                bmob_shopgouwuche.save(new SaveListener<String>() {
                                    @Override
                                    public void done(String s, BmobException e) {
                                        if(e==null){
                                            Toast.makeText(context, "添加进购车啦"+s, Toast.LENGTH_SHORT).show();
                                        }else{
                                            s_int = 0 ;
                                            Toast.makeText(context, "购物车异常", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        }else {
                            s_int =0;
                            Toast.makeText(context, "购物车异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                //点击++后显示减减
                if(shop_dates_ios.get(position).getAdd_shop() != 0){
                    holder.shop_jian.setVisibility(View.VISIBLE);
                }else{
                    holder.shop_jian.setVisibility(View.GONE);
                }
            }
        });
        //减减
        holder.shop_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                minuss(v);
            }
            //减减
            private void minuss(View view) {
                Tools.zoomView(view);
                int s=shop_dates_ios.get(position).getAdd_shop();
                shop_dates_ios.get(position).setAdd_shop(--s);
                if(s > 0){
                    holder.shop_number.setText(shop_dates_ios.get(position).getAdd_shop()+"");
                }else{
                    shop_dates_ios.get(position).setAdd_shop(0);
                    holder.shop_number.setText("0");
                    holder.shop_jian.setVisibility(View.GONE);
                }
                Bmob_shopgouwuche bmob_shopgouwuche=new Bmob_shopgouwuche();
                bmob_shopgouwuche.setShop_imager_address(shop_dates_ios.get(position).getImager_address());
                bmob_shopgouwuche.setShop_sqlitedt_pic(shop_dates_ios.get(position).getPic());
                bmob_shopgouwuche.setShop_sqlitename(shop_dates_ios.get(position).getName());
                bmob_shopgouwuche.setShop_sqliteyueshou(shop_dates_ios.get(position).getYueshou());
                bmob_shopgouwuche.setShop_sqlitenumber(s+"");
                if(s==0){
                    bmob_shopgouwuche.setObjectId(id_shop_ID);
                    bmob_shopgouwuche.delete(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e==null){
                            }else{
                                Toast.makeText(context, "异常状态："+e, Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else{
                    bmob_shopgouwuche.update(id_shop_ID, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e==null){
                            }else{
                                Toast.makeText(context, "异常"+e, Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });
        //显示详情：
        holder.linearlayout_xiangxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog bgSetDialog;
                bgSetDialog = new Dialog(context, R.style.BottomDialogStyle);
                //填充对话框的布局
                View view = LayoutInflater.from(context).inflate(R.layout.tanchaung, null);
                //初始化控件
                ImageView imageView=view.findViewById(R.id.imager);
                //设置数据
                Glide.with(context).load(shop_dates_ios.get(position).getImager_address()).placeholder(R.mipmap.emei_looper2).into(imageView);


                //将布局设置给Dialog
                bgSetDialog.setContentView(view);
                //获取当前Activity所在的窗体
                Window dialogWindow = bgSetDialog.getWindow();
                //设置Dialog从窗体底部弹出
                dialogWindow.setGravity( Gravity.BOTTOM);
                //获得窗体的属性
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                DisplayMetrics dm = new DisplayMetrics();
                //从默认显示器中获取显示参数保存到dm中
                wm.getDefaultDisplay().getMetrics(dm);
                lp.width = (int) (dm.widthPixels*0.95);
                lp.y = 20; //设置Dialog距离底部的距离
                dialogWindow.setAttributes(lp); //将属性设置给窗体
                bgSetDialog.show();//显示对话框


            }
        });
    }

    @Override
    public int getItemViewType(int position) {


        return position;
    }

    @Override
    public int getItemCount() {
        return shop_dates_ios.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView shop_dates_image;
        private TextView shop_dates_name,shop_dates_yueshou,shop_dates_zan,shop_dates_beizhu,shop_dates_pic,shop_number;
        private LinearLayout shop_dates_adds_gwc,shop_jian,linearlayout_xiangxi;
        boolean sw=false;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            linearlayout_xiangxi=itemView.findViewById(R.id.linearlayout_xiangxi);
            shop_dates_name=itemView.findViewById(R.id.shop_dates_name);
            shop_dates_yueshou=itemView.findViewById(R.id.shop_dates_yueshou);
            shop_dates_zan=itemView.findViewById(R.id.shop_dates_zan);
            shop_dates_beizhu=itemView.findViewById(R.id.shop_dates_beizhu);
            shop_dates_image=itemView.findViewById(R.id.shop_dates_image);
            shop_dates_adds_gwc=itemView.findViewById(R.id.shop_dates_adds_gwc);
            shop_dates_pic=itemView.findViewById(R.id.shop_dates_pic);
            shop_jian=itemView.findViewById(R.id.shop_jian_li);
            shop_number=itemView.findViewById(R.id.shop_number);
        }
    }
}
